﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using System.Xml.Serialization;
using System.Diagnostics;
using VSSTQH.Models;
using VSSTQH.Properties;
using VSSTQH.DataAccess;

namespace VSSTQH
{
    public partial class FrmLogin : Form
    {
        #region Ctors
        public FrmLogin()
        {
            InitializeComponent();

            this.btnLogin.Click += new EventHandler(delegate
            {
                CheckLogin();
            });

            this.btnExit.Click += new EventHandler(delegate { this.Close(); });

            TextReader textReader = null;
            try
            {
                // Check is remeber
                if (File.Exists(Common.RememberPath))
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Remember));

                    Remember remember = null;

                    textReader = new StreamReader(Common.RememberPath);
                    remember = xmlSerializer.Deserialize(textReader) as Remember;
                    textReader.Close();

                    this.uiCheckBox1.Checked = true;
                    this.edtUsername.Text = remember.Username;
                    this.edtPassword.Text = remember.Password;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                if (File.Exists(Common.RememberPath))
                {
                    if (textReader != null) textReader.Close();
                    File.Delete(Common.RememberPath);
                }
            }
        }
        #endregion

        #region Methods

        private void CheckLogin()
        {
            if (Authorize())
            {
                if (String.Compare(Common.UserLogin.Username, Common.AdminAccount, true) == 0)
                {
                    Common.UserLogin.GroupID = 1;

                }
                // Load default value
                // Common.LoadSerialPortObject();

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                Common.ShowAlert(Resources.UserInvalid);
            }
        }

        private bool Authorize()
        {
            bool result = false;
            string username = this.edtUsername.Text;
            string password = this.edtPassword.Text;
            if (String.Compare(username, Common.defaultUsername, true) == 0 &&
                String.Compare(this.edtPassword.Text, Common.defaultPassword, true) == 0)
            {
                Common.UserLogin = new UserModel();
                Common.UserLogin.Username = Common.defaultUsername;
                Common.UserLogin.GroupID = 1;

                result = true;
            }
            else
            {
                IUserDAO dao = new UserDAO();
                Common.UserLogin = new UserModel();
                Common.UserLogin.Username = username;
                Common.UserLogin.Password = Encrypt.Encode("MD5", password);
                result = dao.Authentication(ref Common.UserLogin);
            }
            if (this.uiCheckBox1.Checked)
            {
                Remember remember = new Remember();
                remember.Username = this.edtUsername.Text;
                remember.Password = this.edtPassword.Text;

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(Remember));
                TextWriter textWriter = new StreamWriter(Common.RememberPath);
                xmlSerializer.Serialize(textWriter, remember);
                textWriter.Close();
            }
            else
            {
                if (File.Exists(Common.RememberPath))
                    File.Delete(Common.RememberPath);
            }

            return result;
        }

        #endregion
    }
}
